#ifndef BANGOLOGGER_H
#define BANGOLOGGER_H

#include "BangoNokiaQTLibrary_global.h"
#include "BangoFieldNames.h"
#include "BangoConstants.h"
#include "BangoStatic.h"
#include "BangoSessionEvents.h"
#include "BangoNameValuePair.h"
#include "BangoProfile.h"
#include "BangoEvent.h"
#include "BangoStorage.h"
#include "serializer.h"

#include <QGeoPositionInfo>
#include <QGeoPositionInfoSource>
#include <QUrl>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QNetworkConfiguration>
#include <QEventLoop>
#include <QDateTime>
#include <QSysInfo>
#include <QSystemDeviceInfo>
#include <QSystemNetworkInfo>
#include <QMutex>

using namespace QtMobility; //QTM_USE_NAMESPACE
class BangoLogger : public QObject
{
    Q_OBJECT

private:
    QString _applicationId;
    QString _userAgent;
    QString _userId;

    BangoProfile _bangoProfile;
    QVector<BangoEvent*> _bangoEvents;

    bool _logEvents;
    int _sessionResumePeriod;
    QString _postURL;
    bool _useOfflineStorage;
    bool _findLocation;
    QString _locationSource;
    QString _applicationType;
    QString _applicationChannel;
    QString _applicationVersion;

    bool _isRoaming;

    QString _networkName;
    QString _networkCountry;
    QString _networkId;

    QMutex postcurrentMutex;
    QMutex postsavedMutex;
    QMutex savecurrentMutex;

    int LogInternalError(exception& ex, int errorLevel);
    QString GetJSONString(BangoProfile bangoProfile, QVector<BangoEvent*> bangoEvents);
    bool PostEvents(QString jsonString);
    void ClearEvents();

    void GetLoggerProperties();
    QPointer<QGeoPositionInfoSource> _locationDataSource;
    void GetLocation();

public:

    BangoLogger();
    ~BangoLogger();

    void LogEvents(bool logEvents){ _logEvents = logEvents;}
    bool LogEvents(){return _logEvents;}
    void SessionResumePeriod(int sessionResumePeriod){_sessionResumePeriod = sessionResumePeriod; }
    int SessionResumePeriod(){ return _sessionResumePeriod; }
    void ApplicationType(QString applicationType){_applicationType = applicationType; }
    QString ApplicationType(){ return _applicationType; }
    void ApplicationVersion(QString applicationVersion){_applicationVersion = applicationVersion; }
    QString ApplicationVersion(){ return _applicationVersion; }
    void ApplicationChannel(QString applicationChannel){_applicationChannel = applicationChannel; }
    QString ApplicationChannel(){ return _applicationChannel; }
    void UserID(QString userID){_userId = userID;}
    QString UserID(){return _userId; }
    void UserAgent(QString userAgent){_userAgent = userAgent;}
    QString UserAgent(){return _userAgent; }
    void PostURL(QString postURL){_postURL = postURL;}
    QString PostURL(){return _postURL;}
    void UseOfflineStorage(bool useOfflineStorage){_useOfflineStorage = useOfflineStorage;}
    bool UseOfflineStorage(){return _useOfflineStorage;}
    void FindLocation(bool findLocation){_findLocation = findLocation;}
    bool FindLocation(){return _findLocation;}
    void LocationSource(QString locationSource){_locationSource = locationSource;}
    QString LocationSource(){return _locationSource;}

    void InitLogger(QString applicationID);
    int LogEvent(QString eventName, int errorLevel, QString eventValue, QString eventDetail, QString errorId, QMap<QString,QString> eventParameters);
    int LogEvent(QString eventName, QString eventValue, QString eventDetail, QMap<QString,QString> eventParameters);
    int LogEvent(QString eventName, QString eventValue, QString eventDetail);
    int LogEventStart(QString eventValue, QString eventDetail, QMap<QString,QString> eventParameters);
    int LogEventEnd(QString eventValue, QString eventDetail, QMap<QString,QString> eventParameters);
    int LogEventError(QString errorId, QString message, QString errorClass, QMap<QString,QString> eventParameters);
    int LogInternalErrorCritical(exception& ex);
    int LogInternalErrorWarning(exception& ex);

    QString ViewBatch();

    int SaveCurrentEvents();
    void PostStoredEvents();
    void PostCurrentEvents();

public slots:
    void positionUpdated(QGeoPositionInfo geoPositionInfo);
};

#endif // BANGOLOGGER_H
